<?php
include_once ('../../php/postgre.php');
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];
if ($act == 'simpan_subbid') {
    $id_skpd = $_POST['id_subbidang'];
    $id_bidang = $_POST['id_bdgbapeda'];
    $kode = $_POST['kode_subbidang'];
    $nama_subbidang = $_POST['nama_subbidang'];



    if (!is_numeric($id_skpd) || !is_numeric($id_skpd)) {
        echo "Err : invalid id.";
        exit;
    }

    if ($id_skpd > 0) {
        exec_query("update mus_subbidangbappeda set id_subbidangbappeda=" . $id_skpd . ", id_bdgbapeda=" . $id_bidang . ", kode_subbidang='" . $kode . "', nama_subbidang='" . $nama_subbidang . "' where id_subbidangbappeda=" . $id_skpd);
        echo 'success__';
		logs('Mengubah master Sub Bidang Bappeda','Nama Subbidang : '.$nama_subbidang.';');
    } else {
        $maxid = get_maxid('id_subbidangbappeda', 'mus_subbidangbappeda');
        exec_query("insert into mus_subbidangbappeda (id_subbidangbappeda, id_bdgbapeda, kode_subbidang, nama_subbidang) values(" . $maxid . "," . $id_bidang . ", '" . $kode . "', '" . $nama_subbidang . "')");
        $st = get_data('select id_subbidangbappeda from mus_subbidangbappeda where id_subbidangbappeda=' . $maxid);
        if ($st['id_subbidangbappeda'] == $maxid) {
            echo 'success__';
            $id_skpd = $maxid;
			logs('Menambah master Sub Bidang Bappeda','Nama Subbidang : '.$nama_subbidang.';');
        }
    }

    echo $id_skpd . '__';
    view_subbid();
} else if ($act == 'hapus_subbid') {
    $id_skpd = $_POST['id_subbidang'];
    if (is_numeric($id_skpd)) {
		$subb = get_data('select nama_subbidang from mus_subbidangbappeda where id_subbidangbappeda='.$id_skpd);
        exec_query("delete from mus_subbidangbappeda where id_subbidangbappeda=" . $id_skpd);
        echo 'success__';
		logs('Menghapus master Sub Bidang Bappeda','Nama Subbidang : '.$subb['nama_subbidang'].';');
    } else {
        echo 'error';
    }
    view_subbid();
} else if ($act == 'ubah_subbid') {
    $id_skpd = $_POST['id_subbidang'];
    if (is_numeric($id_skpd)) {
        $data = get_data("select id_subbidangbappeda, id_bdgbapeda, kode_subbidang, nama_subbidang from mus_subbidangbappeda where id_subbidangbappeda=" . $id_skpd);
        $ress = implode($data, '__');
        print_r($ress);
	    } else {
        echo 'error';
    }
    
}

function view_subbid() {
    $no = 1;
     $bidang = get_datas("select * from mus_bidang_bappeda order by id_bdgbapeda");
			$idbdf = " ";
			foreach ($bidang as $rw1) {
				$id1 = $rw1['id_bdgbapeda'];
				if($idbdf != $id1){
					$idbdf = $id1;
					?><tr>
						<td><?php echo $id1 ?></td>
						<td><b><?php echo $rw1['nama_bdgbapeda'] ?></b></td>
						<td>&nbsp;</td>
					</tr>
					<?php
					$subbid = get_datas("select * from mus_subbidangbappeda where id_bdgbapeda = " . $rw1['id_bdgbapeda'] . "");
					foreach ($subbid as $rw2){
						?><tr>
							<td class="center"><?php echo $rw2['kode_subbidang'] ?></td>
							<td><?php echo $rw2['nama_subbidang'] ?></td>
							<td class="center">
								<a href="#modalwin" data-toggle="modal"  class="btn btn-info btn-small bt-edit"  name="<?php echo $rw2['id_subbidangbappeda'] ?>"><i class="icon-edit icon-white"></i> ubah</a>
								<a class='btn btn-danger btn-small bt-hapus' name="<?php echo $rw2['id_subbidangbappeda'] ?>"><i class="icon-trash icon-white"></i> hapus</a>   
							</td>
						</tr>
						<?php
						$no++;
					}
				//		$no++;
					//$no++;
				}
            }
}
?>

